﻿namespace SERFLITEMAX20
{
    using MySql.Data.MySqlClient;
    using System;
    using System.Data;

    public class DaAccess
    {
        private MySqlDataAdapter dsCommand;
        private MySqlConnection mConnection = null;
        private string mConnectionString = null;

        public DaAccess(string pConnectionstring)
        {
            this.mConnectionString = pConnectionstring;
            this.dsCommand = new MySqlDataAdapter();
            this.dsCommand.SelectCommand = new MySqlCommand();
            this.mConnection = new MySqlConnection(this.mConnectionString);
            this.dsCommand.SelectCommand.Connection = this.mConnection;
        }

        public DataTable Execute(string commandText)
        {
            DataTable dataTable = new DataTable();
            MySqlCommand selectCommand = this.dsCommand.SelectCommand;
            selectCommand.CommandText = commandText;
            selectCommand.CommandType = CommandType.Text;
            this.dsCommand.Fill(dataTable);
            return dataTable;
        }

        public void ExecuteNonQuery(string commandText)
        {
            try
            {
                MySqlCommand command = null;
                command = new MySqlCommand(commandText, this.mConnection);
                if (this.mConnection.State != ConnectionState.Open)
                {
                    this.mConnection.Open();
                }
                command.ExecuteNonQuery();
                this.mConnection.Close();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public DataTable GetAreaTableFromDbc()
        {
            return this.GetAreaTableFromDbc("");
        }

        public DataTable GetAreaTableFromDbc(string pCriteria)
        {
            return this.Execute("Select `Field1`, `Field2`, `Field3`, `Field12` from `areatable` " + pCriteria);
        }

        public DataTable GetMapTableFromDbc(string pCriteria)
        {
            return this.Execute("Select `Field1`, `Field2`  from  `map` " + pCriteria);
        }
    }
}

